import { Body, Controller, Get, Post, Query } from '@nestjs/common';
import { ApiExtraModels, ApiOperation, ApiTags } from '@nestjs/swagger';
import { RolePageDto } from './role.dto';
import { PageRespData } from '@/common/response.modal';
import { SysRole } from '@/entities/admin/sys-role.entity';
import { SysRoleService } from './role.service';
import {
  RoleCreateDto,
  RoleDeleteDto,
  RoleInfoDto,
  RoleUpdateDto,
  AuthRoleMenuDto
} from './role.dto';
import { ApiException } from '@/common/exceptions/api.exception';
import { RoleInfo, RoleNemu } from './role.modal';
import { SysMenuService } from '../menu/menu.service';
import { ApiResult } from '@/common/decorators/api-result.decorator';
import { ErrorEnum } from '@/common/constants/error';
import { ApiSecurityAuth } from '@/common/decorators/swagger.decorator';
import { AuthUser } from '@/common/decorators/auth-user.decorator';

import * as _ from 'lodash';

@ApiTags('System - 角色模块')
@ApiSecurityAuth()
@ApiExtraModels(RoleInfo)
@Controller('role')
export class SysRoleController {
  constructor(
    private roleService: SysRoleService,
    private menuService: SysMenuService,
  ) {}

  @ApiOperation({ summary: '获取角色列表' })
  @ApiResult({ type: [SysRole] })
  @Get('list')
  async list(): Promise<SysRole[]> {
    return await this.roleService.list();
  }

  @ApiOperation({ summary: '分页查询角色信息' })
  @ApiResult({ type: [SysRole] })
  @Get('page')
  async page(@Query() dto: RolePageDto): Promise<PageRespData<SysRole>> {
    return await this.roleService.page(dto);
  }

  @ApiOperation({ summary: '获取角色信息' })
  @ApiResult({ type: RoleInfo })
  @Get('info')
  async info(@Query() dto: RoleInfoDto): Promise<RoleInfo> {
    return await this.roleService.info(dto.id);
  }

  @ApiOperation({ summary: '新增角色' })
  @Post('add')
  async add(
    @AuthUser('uid') uid: number,
    @Body() dto: RoleCreateDto
  ): Promise<void> {
    // dto.creatorId = uid;
    // await this.roleService.add(dto);
    const roleInfo = await this.roleService.getRoleInfoByName(dto.name);
    console.log(roleInfo, _.isEmpty(roleInfo));
    if (_.isEmpty(roleInfo)) {
      console.log('角色不存在走新增');
      // 走新增
      dto.creatorId = uid;
      await this.roleService.add(dto);
    } else {
      throw new ApiException('角色已存在, 请确认！');
    };
  }

  @ApiOperation({ summary: '更新角色' })
  @Post('update')
  async update(
    @AuthUser('uid') uid: number,
    @Body() dto: RoleUpdateDto
  ): Promise<void> {
    dto.updaterId = uid;
    await this.roleService.update(dto);
    await this.menuService.refreshOnlineUserPerms();
  }

  @ApiOperation({ summary: '删除角色' })
  @Post('delete')
  async delete(@Body() dto: RoleDeleteDto): Promise<void> {
    const count = await this.roleService.countUserIdByRole(dto.ids);
    if (count > 0) {
      throw new ApiException(ErrorEnum.CODE_1008);
    }
    await this.roleService.delete(dto.ids);
    await this.menuService.refreshOnlineUserPerms();
  }

  @ApiOperation({ summary: '更加角色id获取角色权限菜单' })
  @ApiResult({ type: RoleNemu })
  @Get('getMenuByRoleId')
  async getRoleMenuByRoleId(@Query() dto: RoleInfoDto): Promise<RoleNemu> {
    return await this.roleService.getRoleMenuByRoleId(dto.id);
  }

  @ApiOperation({ summary: '新增角色' })
  @Post('authRoleMenu')
  async authRoleMenu(
    @Body() dto: AuthRoleMenuDto
  ): Promise<void> {
    // dto.creatorId = uid;
    // await this.roleService.add(dto);
    // 走新增
    await this.roleService.authRoleMenu(dto);
  }
}
